/*****************************************************************************
 * Product: 数据同步解决方案                                                    
 * Copyright (C) 2010-2011  瑞科软件. All Rights Reserved.                                                                       *
 * 改程序是开源的，您可以修改源代码，做二次开发，定制您自己的同步程序                     
 * 您可以通过我们的网站了解详细信息，包括安装与使用指南，  http://www.ruike.org         
 * 																		      			
 *****************************************************************************/
package com.xml;

import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

public class ExtractFactory {

	public static RecordExtract recordExtract = new RecordExtract();
	public static SQLRecordExtract sQLRecordExtract = new SQLRecordExtract();
	
	public static TextSQLRecordExtract textSQLRecordExtract = new TextSQLRecordExtract();
	
	/**
	 * 判断类型
	 * 
	 * @param html
	 * @param storeFrom
	 * @return
	 * @throws Exception
	 *             Jul 14, 2009
	 */
	public static boolean isPO(String html) throws Exception {

		if (html.indexOf("TEXT:") == 0) {
			return false;
		}
		if (html.indexOf("DIV") > 0) {
			return true;
		}
		boolean flag = true;
		Parser parser = new Parser();
		parser.setInputHTML(html);
		parser.setEncoding("gb2312");
		// 迭代所有节点, null 表示不使用 NodeFilter
		TagNameFilter tf = new TagNameFilter("TABLE");
		NodeList list = parser.parse(tf);
		// 取得Map的条数
		TableTag tn = (TableTag) list.elementAt(0);
		String type = tn.getAttribute("type");
		if (type == null) {
			return true;
		}
		return type.equalsIgnoreCase("po");
	}

	public static Extract getExtract(String html) {
		if (html.indexOf("TEXT:") == 0) {			
			return textSQLRecordExtract;
		}
		if (html.indexOf("DIV") > 0) {			
			return recordExtract;
		}
		boolean flag = true;
		Parser parser = new Parser();
		try {
			parser.setInputHTML(html);
			parser.setEncoding("gb2312");
			// 迭代所有节点, null 表示不使用 NodeFilter
			TagNameFilter tf = new TagNameFilter("TABLE");
			NodeList list = parser.parse(tf);
			// 取得Map的条数
			TableTag tn = (TableTag) list.elementAt(0);
			String type = tn.getAttribute("type");
			if (type == null) {				
				return recordExtract;
			}
			if (type.equalsIgnoreCase("po")) {				
				return recordExtract;
			} else {				
				return sQLRecordExtract;
			}
		} catch (ParserException e) {
			e.printStackTrace();
		}
		return null;
	}

}
